package com.yilin.parents.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.activity.common.ShareDialogActivity;
import com.molyfun.parents.uievent.EventDispatcherEnum;
import com.molyfun.parents.utils.UIUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:4/11/17.
 */

public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.pay_result);

        api = WXAPIFactory.createWXAPI(this, ShareDialogActivity.WX_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(final BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    int result = 0;
                    switch (resp.errCode) {
                        case BaseResp.ErrCode.ERR_OK:
                            UIUtils.showToast(getApplicationContext(), "支付成功");
                            Message uimessage = new Message();
                            uimessage.what = EventDispatcherEnum.UI_EVENT_WECHAT_PAY_SUCCESS;
                            MFPApplication.getInstance().getEventDispatcher().sendMessage(uimessage);
                            break;
                        case BaseResp.ErrCode.ERR_USER_CANCEL:
                            UIUtils.showToast(getApplicationContext(), "支付取消");
                            break;
                        case BaseResp.ErrCode.ERR_AUTH_DENIED:
                            UIUtils.showToast(getApplicationContext(), "支付被拒绝");
                            break;
                        case BaseResp.ErrCode.ERR_UNSUPPORT:
                            UIUtils.showToast(getApplicationContext(), "不支持错误");
                            break;
                        default:
                            UIUtils.showToast(getApplicationContext(), "支付出错");
                            break;
                    }
                }
            });

        }
        finish();
    }
}